<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>东财主力排名</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">				
			//p {margin:5px}
			//ul {margin:1px}			
			//h4 {margin:1px}	
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			table{
			width:460px; 
			border-collapse:collapse; 
			text-align:center;
			}
			td,th{border:1px solid #696969}			
		</style>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			window.onload = function() {
			getSource(1);			
			changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource(1);						
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","15:15")						
				},
				60000);
			})
						
			function changeStyle() {
				var obj1 = document.getElementById("towrite");				
				obj1.style.left= "0px"; 				
				obj1.style.width= "480px";	
				obj1.style.height= "620px";		
				obj1.style.fontSize="12px";						
			}	
						
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}						
			
			var trid = "";
			function changeCss(obj,hid,id){						
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					tlist[i].style.backgroundColor = "#252528"; 					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background="#252528";
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}			
			
			function getSource(ss) {			
				div = document.getElementById('towrite');								
				var	hs = "<a href='javascript:void(0)'  id='bt1' onclick='getSource(1)'>沪深A股</a>";
				var	cy = "<a href='javascript:void(0)'  id='bt2' onclick='getSource(2)'>创业板</a>";				
				div.innerHTML = "<font size='2'><strong>东财主力排名</strong><font size='1'>&nbsp;&nbsp;&nbsp;&nbsp;（" + hs + cy + "）</font></font><p>";				
				aObj = document.getElementById('bt' + ss);				
				aObj.style.color = "#FFA07A";	
				var url1 = "http://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112308980107330704572_1632445353427&fid=f184&po=1&pz=100&pn=1&np=1&fltt=2&invt=2&fields=f2,f3,f12,f14,f100,f184,f225&ut=b2884a393a59ad64002292a3e90d46a5&fs=m:0+t:6+f:!2,m:0+t:13+f:!2,m:0+t:80+f:!2,m:1+t:2+f:!2,m:1+t:23+f:!2"; 	                							
				var url2 = "http://push2.eastmoney.com/api/qt/clist/get?cb=jQuery1123009706366557087875_1632575677122&fid=f184&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&fields=f2,f3,f12,f14,f100,f184,f225&ut=b2884a393a59ad64002292a3e90d46a5&fs=m:0+t:80+f:!2"; 	                							
				var url = url1;
				if(ss == 2){
					url = url2;
				}
				if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();				
				} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");				
				}					
				xmlHttp.onreadystatechange = writeSource;
				xmlHttp.open("GET", url, true);				
				//xmlHttp.withCredentials = true
				xmlHttp.send(null);				
			}
						
			function writeSource() { 
				if (xmlHttp.readyState == 4) {         
					var data = xmlHttp.responseText					
					data = data.replace(/ /g,"").match(/\"diff\":(\S*)\]\}\}\);/)[1];	
					//data = "{\"data\":" + data + "}]}";
					
					data = data.replace(/(\{\").*?(?=\})/g,function(word){ 				
						str = word.match(/\{(\S*)/)[1];			
						return "{" + str.split(',')[2] + "," +			
						str.split(',')[3] + ","	+	
						str.split(',')[6] + ","	+	 
						str.split(',')[0] + ","	+	 
						str.split(',')[1] + ","	+						
						str.split(',')[5] + ","	+	 
						str.split(',')[4] ;
					});
					data = data + "]";
					//document.write(data);
					
					var json = JSON.parse(data);
					//document.write(json);
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var n = 2;
					var b = 0;
					for(var key in json[0]){  //表头
						key = key.replace('f225','排名').replace('f12','代码').replace('f14','名称').replace('f3','涨幅%').replace('f2','现价');
						key = key.replace('f184','主力净占比%').replace('f100','板块');					
						
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);						
						if(key == "板块"){
							b = m;
							th.style.color = "#FFA500";	
						}
						m++;
					}
										
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						var mc = "";
						for(var key in json[i]){ //行元素遍历						
							var td=document.createElement("td");							
							
							var rc = json[i][key];
							if(p == 1){  //代码列
								dm = rc;
								tr.id = dm;
							}					
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + dm + "\",\"gg\")'>" + rc + "</a>";
							
							if(p == n + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FF6633";								
							}
							if(p == b + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FFA500";								
							}							
							tr.appendChild(td);
							p++;
						}						
						tbody.appendChild(tr);
					}					
					var div = document.getElementById("towrite");									
					div.appendChild(table);
					table.id = "gg";
					
					var table=document.getElementsByTagName("table")[0];
					makeSortable(table);
					hover();					
				}
			}
			
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){
							//alert(n);						
							showCol(n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("板块") != -1){
									val1 = val1;
									val2 = val2;
								}else{
									if(val1 == "-"){
										val1 = " ";
									}
									if(val2 == "-"){
										val2 = " ";
									}
									val1 = val1.replace(" ","0").replace("无","1000").replace("NaN","0");
									val2 = val2.replace(" ","0").replace("无","1000").replace("NaN","0");
									if(val1.indexOf("万") != -1){
										val1 = parseFloat(val1)*10000;
									}else if(val1.indexOf("亿") != -1){
										val1 = parseFloat(val1)*100000000;
									}else{
										val1 = parseFloat(val1);
									}
									
									if(val2.indexOf("万") != -1){
										val2 = parseFloat(val2)*10000;
									}else if(val2.indexOf("亿") != -1){
										val2 = parseFloat(val2)*100000000;
									}else{
										val2 = parseFloat(val2);
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(iCol){			
				var oTable = document.getElementsByTagName('table')[0];
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){
						oTable.rows[i].cells[j].style.color = "#d2d2d3";
						
						oTable.rows[i].cells[6].style.color = "#FFA500"; 										
					}
					oTable.rows[i].cells[iCol].style.color = "#FF6633";					
				}

				otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					otagsA[i].style.color = "#d2d2d3";
					if(otagsA[i].id == 7){
						otagsA[i].style.color = "#FFA500";								
					}
					if(otagsA[i].id == iCol + 1){
						otagsA[i].style.color = "#FF6633";
					}
				}								
			}
		</script>
	</head>
	<body>
		<div id="towrite" class="fixedBox"></div>		
	</body>
</html>